package com.beiding.jsonanalyzer.analyze.query;

import com.beiding.jsonanalyzer.analyze.func.FuncBase;
import com.beiding.jsonanalyzer.flow.ValueNode;

import java.util.Iterator;
import java.util.List;

/**
 * 比较器
 * 本身也是函数
 */
class BaseQuery implements FuncBase<ComparatorResult> {

    /**
     * 条件列表
     */
    private List<List<FuncBase>> orList;

    BaseQuery(List<List<FuncBase>> orList) {
        this.orList = orList;
    }

    @Override
    public boolean checkAgg() {
        return false;
    }

    @Override
    public ComparatorResult executeOne(ValueNode valueNode) {

        //遍历or比较单元
        for (List<FuncBase> andList : orList) {
            Object comparatorResult = ComparatorResult.TRUE;

            //执行单个and条件的比较
            for (FuncBase condition : andList) {
                comparatorResult = condition.executeOne(valueNode);
                if (comparatorResult == ComparatorResult.FALSE || comparatorResult == null) {
                    break;
                }
            }

            if (comparatorResult == ComparatorResult.TRUE || comparatorResult == null) {
                return (ComparatorResult) comparatorResult;
            }
        }

        //如果没有任何or子句执行通过返回0,表示整个句子执行不通过
        return ComparatorResult.FALSE;
    }

    @Override
    public void pugAgg(ValueNode valueNode) {
        for (List<FuncBase> funcBases : orList) {
            for (FuncBase funcBase : funcBases) {
                funcBase.pugAgg(valueNode);
            }
        }
    }

    @Override
    public void resetAgg() {
        for (List<FuncBase> funcBases : orList) {
            for (FuncBase funcBase : funcBases) {
                funcBase.resetAgg();
            }
        }
    }

    @Override
    public ComparatorResult aggGet() {
        //遍历or比较单元
        for (List<FuncBase> andList : orList) {
            Object comparatorResult = ComparatorResult.TRUE;

            //执行单个and条件的比较
            for (FuncBase condition : andList) {
                comparatorResult = condition.aggGet();
                if (comparatorResult == ComparatorResult.FALSE || comparatorResult == null) {
                    break;
                }
            }

            if (comparatorResult == ComparatorResult.TRUE || comparatorResult == null) {
                return (ComparatorResult) comparatorResult;
            }
        }

        //如果没有任何or子句执行通过返回0,表示整个句子执行不通过
        return ComparatorResult.FALSE;
    }

    @Override
    public String toString() {

        if (orList.size() == 0) {
            return "true";
        }

        if (orList.size() == 1) {
            return andText(orList.get(0));
        }

        StringBuilder builder = new StringBuilder();
        Iterator<List<FuncBase>> iterator = orList.iterator();
        builder.append("(");

        if (iterator.hasNext()) {
            builder.append(andText(iterator.next()));
            while (iterator.hasNext()) {
                builder.append(" or ").append(andText(iterator.next()));
            }
        }
        builder.append(")");

        return builder.toString();
    }

    private String andText(List<FuncBase> list) {
        if (list.size() == 0) {
            return "true";
        }
        if (list.size() == 1) {
            return list.get(0).toString();
        }
        StringBuilder builder = new StringBuilder();
        Iterator<FuncBase> iterator = list.iterator();

        if (iterator.hasNext()) {
            builder.append(iterator.next());
            while (iterator.hasNext()) {
                builder.append(" and ").append(iterator.next());
            }
        }

        return builder.toString();

    }

}
